﻿using Microsoft.Extensions.Logging;
using System.Linq.Expressions;
using XYAC.Framework.WohuaMesExtend.IHI.OracleRemoteDB;

namespace XYAC.Framework.WohuaMesExtend.IHI.Services
{
    public class YB_LOT_INSPECTService
    {
        private const string __REMOTE_SYNC_STATE_PENDING = "PENDING";
        private const string __REMOTE_SYNC_STATE_COMPLETE = "COMPLETE";

        public YB_LOT_INSPECTService(OracleMesContext db, ILogger<YB_TechnologyService> logger)
        {
            this.Db = db;
            this.Logger = logger;
        }

        public OracleMesContext Db { get; }
        public ILogger<YB_TechnologyService> Logger { get; }

        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNoList"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        internal IEnumerable<YB_LOT_INSPECT> GetSnData(string[] machineNoList, string state = __REMOTE_SYNC_STATE_PENDING)
        {
            var snList = this.Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
            return snList;
        }
        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNo"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        internal IEnumerable<YB_LOT_INSPECT> GetSnData(string machineNo, string state = __REMOTE_SYNC_STATE_PENDING)
        {
            var snList = this.Db.YB_LOT_INSPECT.Where(T => machineNo == T.EQP && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
            return snList;
        }

        public IEnumerable<YB_LOT_INSPECT> GetSnPage(
            int pageIndex = 0,
            int rowCount = 50,
            Expression<Func<YB_LOT_INSPECT, bool>> filterCondition = null,
            Expression<Func<YB_LOT_INSPECT, object>> orderByCondition = null,
            Expression<Func<YB_LOT_INSPECT, object>> orderByDescCondition = null,
            string state = __REMOTE_SYNC_STATE_PENDING)
        {
            try
            {
                IQueryable<YB_LOT_INSPECT> data = this.Db.Set<YB_LOT_INSPECT>();
                if (filterCondition is not null)
                {
                    data = data.Where(filterCondition);
                }
                if (orderByCondition is not null)
                {
                    data = data.OrderBy(orderByCondition);
                }
                if (orderByDescCondition is not null)
                {
                    data = data.OrderByDescending(orderByDescCondition);
                }
                return data.Skip(pageIndex * rowCount).Take(rowCount).ToList();
            }
            catch (Exception ex)
            {
                Logger.LogError("分页下载中间表数据失败", ex);
                return Enumerable.Empty<YB_LOT_INSPECT>();
            }
        }

        internal IEnumerable<YB_LOT_INSPECT> GetSnData(string? machineNo, DateTime plandate)
        {
            var snList = this.Db.YB_LOT_INSPECT.Where(T => machineNo == T.EQP && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING && T.PLAN_DATE.Value.Date == plandate).ToList();
            return snList;
        }

        internal int UpdateFlag(string rEMOTE_SYNC_STATE_COMPLETE, YB_LOT_INSPECT currentNotifyData)
        {
            currentNotifyData.RECEIVE_FLAG = rEMOTE_SYNC_STATE_COMPLETE;
            this.Db.Update(currentNotifyData);
            return this.Db.SaveChanges();
        }
    }
}
